package com.kfh.ybracelet.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;


/**
 * 修改姓名页面
 * Created by Aaron on 15/8/11.
 */
public class ModifyNameActivity extends BaseActivity {
    //修改我的姓名Code
    private static final int MODIFY_MY_NAME_CODE = 10000;
    private EditText et_name;
    private ImageView iv_delete;

    //    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_modify_name);

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_delete = (ImageView) findViewById(R.id.iv_delete);
        final TextView tv_right = (TextView) findViewById(R.id.tv_right);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_right.setText(getResources().getString(R.string.done));
        tv_title.setText(getResources().getString(R.string.modify_name));
        et_name = (EditText) findViewById(R.id.et_name);
        et_name.setText(sp.getString(Constant.NICKNAME, ""));

        et_name.setSelection(et_name.getText().toString().trim().length());

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        iv_delete.setOnClickListener(this);

        tv_right.setTextColor(getResources().getColor(R.color.line_gray));
        setClick(tv_right, false);

        et_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete.setVisibility(et_name.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                    et_name.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete.setVisibility(et_name.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                            if (sp.getString(Constant.NICKNAME, "").equals(et_name.getText().toString())
                                    || CommonUtils.isNull(et_name.getText().toString().trim())) {

                                tv_right.setTextColor(getResources().getColor(R.color.line_gray));
                                setClick(tv_right, false);

                            } else {

                                tv_right.setTextColor(getResources().getColor(R.color.black));
                                setClick(tv_right, true);
                            }

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                        }
                    });

                } else {
                    iv_delete.setVisibility(View.INVISIBLE);

                }
            }
        });

    }

    private void setClick(TextView tv_right, boolean b) {
        tv_right.setClickable(b);
        tv_right.setFocusable(b);
        tv_right.setEnabled(b);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.iv_delete:
                et_name.setText("");

                InputMethodManager inputManager =
                        (InputMethodManager) et_name.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(et_name, 0);

                break;

            case R.id.tv_right://完成

                if (CommonUtils.isNull(et_name.getText().toString().trim())) {
                    showToast("姓名不能为空");
                    return;
                }

                String modifyname = et_name.getText().toString().trim();
                Intent data = new Intent();
                data.putExtra("modifyname", modifyname);

                setResult(MODIFY_MY_NAME_CODE, data);
                finish();

                break;

        }
    }
//    public void onResume() {
//        super.onResume();
//        MobclickAgent.onResume(this);       //统计时长
//    }

//    public void onPause() {
//        super.onPause();
//        MobclickAgent.onPause(this);
//    }
}
